package cur.nature.starter.common.idgenerator.sequence;

import cur.nature.framework.core.util.StringUtil;
import cur.nature.tinyorm.core.proxy.TemplateProxy;
import cur.nature.tinyorm.core.template.DefaultJdbcTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

/**
 * 用户信息 Dao
 * 作者：Auto Generator By wujian
 * 生成日期：2023-07-10 21:42:14
 */
@Component
@DependsOn(DefaultJdbcTemplate.TEMPLATE)
public class SequenceDao {

    private DefaultJdbcTemplate template;
    private TemplateProxy templateProxy;

    public SequenceDao(@Qualifier(DefaultJdbcTemplate.TEMPLATE) DefaultJdbcTemplate template) {
        this.template = template;
        this.templateProxy = template.getTemplateProxy();
    }

    public int creatSeq(final String seqName) {
        final String sql = StringUtil.format("CREATE SEQUENCE {0} START WITH 1 INCREMENT BY 1",seqName);
        return template.update(sql);
    }

    public boolean existSel(final String seqName) {
        final String sql = StringUtil.format("SELECT count(0) FROM user_sequences WHERE UPPER(sequence_name) = UPPER(''{0}'')", seqName);
        final Long count = template.queryForObject(sql, Long.class);
        return count != null && count > 0;
    }

    public Long querySeq(final String seqName) {
        final String sql = StringUtil.format("SELECT {0}.nextval FROM dual", seqName);
        final Long next = template.queryForObject(sql, Long.class);
        return next;
    }


}

